package cn.tcp;

import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.util.Properties;

import com.serotonin.messaging.WaitingRoomException;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.ModbusRequest;
import com.serotonin.modbus4j.msg.ModbusResponse;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
import com.serotonin.util.queue.ByteQueue;

public class TcpSupport {
	private ModbusMaster tcpModBusMaster;
	protected IpParameters ipParameters;
	private ModbusFactory modbusFactory = new ModbusFactory();
	private boolean tcpMasterAlive = false;
	private String ip;
	private int port;

	public TcpSupport(Properties props) {
		Properties prop = new Properties();
		InputStream in = Object.class.getResourceAsStream("/Modbus.properties");
		try {
			prop.load(in);
			ip = prop.getProperty("ip").trim();
			port = Integer.parseInt(prop.getProperty("port").trim());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TcpSupport(String host, int port, boolean encapsulated) {
		this.ipParameters = new IpParameters();
		ipParameters.setHost(host);
		ipParameters.setPort(port);
		ipParameters.setEncapsulated(encapsulated);

	}

	public TcpSupport() {

	}

	public TcpSupport(IpParameters ipparameter) {
		this.ipParameters = ipparameter;
	}

	public TcpSupport(String host) {
		this.ipParameters = new IpParameters();
		this.ipParameters.setHost(host);
	}

	public TcpSupport(String host, int port) {
		this.ipParameters = new IpParameters();
		ipParameters.setHost(host);
		ipParameters.setPort(port);
		ipParameters.setEncapsulated(false);
	}
     //java动态语句块
	{
		ipParameters=new IpParameters();
		ipParameters.setHost("192.168.1.122");
		ipParameters.setPort(502);
		try {
			tcpModBusMaster = modbusFactory.createTcpMaster(ipParameters,
					tcpMasterAlive);
			tcpModBusMaster.init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送request命令
	 * 
	 * @param request
	 *            发送一个request 的命令
	 * @return ModbusResponse 返回一个response的回应
	 * @throws ModbusTransportException
	 */
	synchronized public ModbusResponse sendModbusRequest(ModbusRequest request) {
		ModbusResponse response = null;
		try {
			response = tcpModBusMaster.send(request);
			Thread.sleep(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

	protected ModbusMaster getTcpModBusMaster() {
		if (tcpModBusMaster == null) {
			tcpModBusMaster = modbusFactory.createTcpMaster(getIpParameters(),
					false);
		}
		return tcpModBusMaster;
	}

	protected void setTcpModBusMaster(ModbusMaster tcpModBusMaster) {
		this.tcpModBusMaster = tcpModBusMaster;
	}

	protected IpParameters getIpParameters() {
		if (ipParameters == null || ipParameters.getHost() == null) {

			throw new RuntimeException("Ip没有被初始化");
		}
		return ipParameters;
	}

	protected void setIpParameters(IpParameters ipParameters) {
		this.ipParameters = ipParameters;
	}

}
